package cn.wolfcode.shop.server.service.impl;

import cn.wolfcode.shop.api.domain.*;
import cn.wolfcode.shop.api.service.ICatalogService;
import cn.wolfcode.shop.api.service.IProductService;
import cn.wolfcode.shop.api.service.IProductSkuService;
import cn.wolfcode.shop.api.service.ISkuPropertyService;
import cn.wolfcode.shop.api.vo.CreateSkuVo;
import cn.wolfcode.shop.server.mapper.ProductSkuMapper;
import cn.wolfcode.shop.server.mapper.ProductSkuPropertyMapper;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.*;

@Service
public class ProductSkuServiceImpl implements IProductSkuService {
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductSkuPropertyMapper productSkuPropertyMapper;
    @Autowired
    private IProductService productService;
    @Autowired
    private ICatalogService catalogService;
    @Autowired
    private ISkuPropertyService skuPropertyService;

    @Override
    public List<ProductSku> creatSKU(CreateSkuVo createSkuVo) {
        //判断商品是否存在:使用productId获取catalog code
        //处理参数
        Product product = productService.get(createSkuVo.getProductId());
        if (product == null) {
            throw new RuntimeException("该商品不存在");
        }
        List<SkuProperty> skuPropertyList = createSkuVo.getSkuPropertyList();
        List<SkuPropertyValue> skuPropertyValueList = createSkuVo.getSkuPropertyValueList();
        //处理参数,将不同分类的sku属性分开
        Map<Long, List<SkuPropertyValue>> map = new HashMap<>();
        for (int i = 0; i < skuPropertyList.size(); i++) {
            SkuProperty skuProperty = skuPropertyList.get(i);
            Long skuPropertyId = skuProperty.getId();
            List<SkuPropertyValue> list = new ArrayList<>();
            map.put(skuPropertyId, list);
        }
        for (SkuPropertyValue skuPropertyValue : skuPropertyValueList) {
            map.get(skuPropertyValue.getSkuProperty().getId()).add(skuPropertyValue);
        }
        //生成sku
        List<ProductSku> productSkuList = new ArrayList<>();
        //  生成前缀分类编号
        String prefix = this.skuPrefixGenerator(product);
        //  生成sku对象列表
        Collection<List<SkuPropertyValue>> values = map.values();
        List<List<ProductSkuProperty>> prodctSkuPropertyList = this.skuPropertyValueGenerator(null, values);
        Integer index = 0;
        for (List<ProductSkuProperty> productSkuProperties : prodctSkuPropertyList) {
            ProductSku sku = new ProductSku();
            sku.setProductSkuPropertyList(productSkuProperties);
            sku.setCode(prefix + index.toString());
            sku.setProductId(product.getId());
            productSkuList.add(sku);
            index++;
        }
        return productSkuList;
    }

    @Override
    public void saveSku(Long productId, List<ProductSku> productSkuList) {
        //查询是否已经生成,是则全部删除
        if (this.isGenerated(productId)) {
            List<Long> skuIdList = productSkuMapper.selectSkuIdByProductId(productId);
            productSkuPropertyMapper.deleteBySkuId(skuIdList);
            productSkuMapper.deleteByProductId(productId);
        }
        //保存product_sku\product_sku_property
        for (ProductSku productSku : productSkuList) {
            // 保存 product_sku
            ProductSku sku = new ProductSku();
            sku.setProductId(productId);
            sku.setCode(productSku.getCode());
            sku.setPrice(productSku.getPrice());
            sku.setMods(productSku.getMods());
            productSkuMapper.insert(sku);
            // 保存 product_sku_property
            List<ProductSkuProperty> productSkuPropertyList = productSku.getProductSkuPropertyList();
            for (ProductSkuProperty skuProperty : productSkuPropertyList) {
                ProductSkuProperty productSkuProperty = new ProductSkuProperty();
                productSkuProperty.setProductSkuId(sku.getId());
                productSkuProperty.setValue(skuProperty.getValue());
                productSkuProperty.setSkuPropertyId(skuProperty.getSkuPropertyId());
                productSkuProperty.setImage(skuProperty.getImage());
                productSkuPropertyMapper.insert(productSkuProperty);
            }
        }
    }

    @Override
    public List<ProductSku> selectSkuByProductId(Long id) {
        List<ProductSku> productSkus = productSkuMapper.selectByProductId(id);
        for (ProductSku sku : productSkus) {
            sku.setProductSkuPropertyList(productSkuPropertyMapper.selectBySkuId(sku.getId()));
        }
        return productSkus;
    }

    @Override
    public BigDecimal selectMinPriceByProductId(Long id) {
        return productSkuMapper.selectMinPriceByProductId(id);
    }

    @Override
    public boolean isGenerated(Long id) {
        if (productSkuMapper.countByProductId(id) == 0) {
            return false;
        }
        return true;
    }



    private String skuPrefixGenerator(Product product) {
        List<Catalog> catalogList = new ArrayList<>();
        Catalog catalog = catalogService.get(product.getCatalog().getId());
        while (true) {
            catalogList.add(catalog);
            Long parentCatalogId = catalog.getParentCatalogId();
            if (parentCatalogId != null) {
                catalog = catalogService.get(parentCatalogId);
            } else {
                break;
            }
        }
        StringBuilder prefix = new StringBuilder();
        prefix.append(catalogList.get(catalogList.size() - 1).getCode().substring(0, 2).toUpperCase());
        for (int i = 2; i <= 3; i++) {
            if (catalogList.size() - i < 0) {
                break;
            }
            String sequence = catalogList.get(catalogList.size() - i).getSequence().toString();
            if (sequence.length() < 2) {
                prefix.append("0" + sequence);
            } else {
                prefix.append(sequence.substring(0, 2));
            }
        }
        prefix.append(product.getId());
        return prefix.toString();
    }

    private List<List<ProductSkuProperty>> skuPropertyValueGenerator(List<List<ProductSkuProperty>> productSkuPropertyList, Collection<List<SkuPropertyValue>> skuPropertyValueList) {
        if (productSkuPropertyList == null) {
            productSkuPropertyList = new ArrayList<>();
        }
        Iterator<List<SkuPropertyValue>> iterator = skuPropertyValueList.iterator();
        if (iterator.hasNext()) {
            List<List<ProductSkuProperty>> result = new ArrayList<>();
            List<SkuPropertyValue> skuPropertyValues = iterator.next();
            if (productSkuPropertyList.size() == 0) {
                for (SkuPropertyValue skuPropertyValue : skuPropertyValues) {
                    List<ProductSkuProperty> productSkuProperties = new ArrayList<>();
                    ProductSkuProperty property = new ProductSkuProperty();
                    property.setValue(skuPropertyValue.getValue());
                    property.setSkuPropertyId(skuPropertyValue.getSkuProperty().getId());
                    productSkuProperties.add(property);
                    result.add(productSkuProperties);
                }
            } else {
                for (SkuPropertyValue skuPropertyValue : skuPropertyValues) {
                    for (List<ProductSkuProperty> productSkuProperties : productSkuPropertyList) {
                        List<ProductSkuProperty> temp = new ArrayList<>(productSkuProperties);
                        ProductSkuProperty property = new ProductSkuProperty();
                        property.setValue(skuPropertyValue.getValue());
                        property.setSkuPropertyId(skuPropertyValue.getSkuProperty().getId());
                        temp.add(property);
                        result.add(temp);
                    }
                }
            }
            iterator.remove();
            productSkuPropertyList = skuPropertyValueGenerator(result, skuPropertyValueList);
        }
        return productSkuPropertyList;
    }
}
